package com.example.lwy.myapplication.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by LWY on 2016/10/10.
 */
public class CustomView extends View implements Runnable {
    private Paint mPaint;
    private int mWidth;
    private int mheight;
    private int radiu;


    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint() {

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.LTGRAY);
        // 设置画笔颜色为自定义颜色
        mPaint.setColor(Color.argb(255, 255, 128, 103));
        mPaint.setColorFilter(new ColorMatrixColorFilter(new float[]{
                0.5f, 0, 0, 0, 0,
                0, 0.5f, 0, 0, 0,
                0, 0, 0.5f, 0, 0,
                0, 0, 0, 0.5f, 0,
        }));
        mPaint.setStrokeWidth(10);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制圆环
        setRadiu(200);
        canvas.drawCircle(mWidth / 2, mheight / 2, radiu, mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mheight = h;
    }

    public void setRadiu(int radiu) {
        this.radiu = radiu;
    }

    @Override
    public void run() {
        try {
            while (true) {
                if (radiu == 200) {
                    radiu = 0;
                } else {
                    radiu += 10;
                    postInvalidate();
                }
                Thread.sleep(40);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();

        }
    }
}
